//
//  ViewController.m
//  Location
//
//  Created by BaBa on 2022/2/22.
//

#import "ViewController.h"
#import "GPXWaypoint.h"
#import "GPXParser.h"
#import "QMUIButton.h"
#import "QMUITextField.h"
#import "Masonry.h"
#import "QMUITips.h"

@interface ViewController ()
@property (nonatomic, strong) QMUITextField *i1;
@property (nonatomic, strong) QMUITextField *i2;
@property (nonatomic, strong) UILabel *label;
@end

@implementation ViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	self.view.backgroundColor = [UIColor cyanColor];
    
    QMUITextField * i1 = [[QMUITextField alloc] init];
    i1.textColor = [UIColor whiteColor];
    i1.placeholderColor = [UIColor whiteColor];
    i1.backgroundColor = [UIColor grayColor];
    i1.keyboardType = UIKeyboardTypeDecimalPad;
    self.i1 = i1;
    i1.placeholder = @"纬度（Latitude）";
    [self.view addSubview:i1];
    [i1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(120);
        make.height.mas_equalTo(44);
    }];
    
    QMUITextField * i2 = [[QMUITextField alloc] init];
    i2.textColor = [UIColor whiteColor];
    i2.placeholderColor = [UIColor whiteColor];
    i2.backgroundColor = [UIColor grayColor];
    i2.keyboardType = UIKeyboardTypeDecimalPad;
    self.i2 = i2;
    i2.placeholder = @"经度（Longitude）";
    [self.view addSubview:i2];
    [i2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(120 + 44 + 20);
        make.height.mas_equalTo(44);
    }];
    
    QMUIButton *button = [[QMUIButton alloc] init];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(120 + 44 + 20 + 44 + 20);
        make.height.mas_equalTo(44);
    }];
    
    self.label = [[UILabel alloc] init];
    self.label.textColor = [UIColor redColor];
    [self.view addSubview:self.label];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.view);
    }];
}

-(void)buttonAction{
    NSString *text1 = self.i1.text;
    NSString *text2 = self.i2.text;
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"LSJLocation" withExtension:@"gpx"];
    [self modifyGPX:fileURL newLatitude:[text1 doubleValue] newLongitude:[text2 doubleValue]];
}


-(void)modifyGPX:(NSURL *)fileURL newLatitude:(CLLocationDegrees)newLatitude newLongitude:(CLLocationDegrees) newLongitude {
    // Read and parse the GPX file
    GPXParser *parser = [[GPXParser alloc] init];
    NSArray<GPXWaypoint *> *waypoints = [parser parseFileAtURL:fileURL];
    
    if (waypoints.count == 0) {
        NSLog(@"No waypoints found");
        return;
    }
    
    GPXWaypoint *waypoint = waypoints[0];
    
    // Modify latitude and longitude
    waypoint.latitude = newLatitude;
    waypoint.longitude = newLongitude;
    
    // Read the XML file content
    NSError *error;
    NSString *xmlString = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        [QMUITips showError:[NSString stringWithFormat:@"Failed to read GPX file: %@", error.localizedDescription]];
        return;
    }
    
    // Modify the XML content
    NSString *modifiedXMLString = [xmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"lat=\"%f\"", waypoint.latitude] withString:[NSString stringWithFormat:@"lat=\"%f\"", newLatitude]];
    modifiedXMLString = [modifiedXMLString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"lon=\"%f\"", waypoint.longitude] withString:[NSString stringWithFormat:@"lon=\"%f\"", newLongitude]];
    
    // Write the modified XML content back to the file
    [modifiedXMLString writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        self.label.text = @"fail";
    } else {
        self.label.text = @"success";
    }
}

@end
